package sample.dao;


import java.util.logging.Logger;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;


public class DAO {
	public static Session getSession(){
		Session session=(Session)DAO.session.get();
		if(session==null){
			session=sessionFactory.openSession();
			DAO.session.set(session);
		}
		return session;
	}
	
	protected void begin(){
		getSession().beginTransaction();
		
	}
	
	protected void commit(){
		getSession().getTransaction().commit();
		
	}
	
	protected void rollback(){
		getSession().getTransaction().rollback();
	
		
		DAO.session.set(null);
	}
	
	public static void close(){
		getSession().close();

		DAO.session.set(null);
	}
	
	private static final Logger log =java.util.logging.Logger.getAnonymousLogger();
	
	private static final ThreadLocal session=new ThreadLocal<>();
	
	private static final SessionFactory sessionFactory=
			new AnnotationConfiguration().configure().buildSessionFactory();
	
	
}
